﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Windows;
using System.Xml;
using GEPSDevelopEnvironment.Common;
using Microsoft.Win32;

namespace GEPSDevelopEnvironment.Config
{
    public sealed class ConfigFile
    {
        private static readonly ConfigFile _instance = new ConfigFile();
        private string _ModifyFileName;
        private string _ConfigFileName;
        private bool _Modified;
        private XmlDocument _ConfigFile;

        private ConfigFile()
        {
            _ModifyFileName = "";
            _ConfigFileName = "";
            _Modified = false;
            CreateNewConfig();
        }

        public void CreateNewConfig()
        {
            _ConfigFile = new XmlDocument();
            _ModifyFileName = Path.GetTempFileName();
            _ConfigFile.LoadXml(Properties.Resources.ConfigTemplate);
            _ConfigFile.Save(_ModifyFileName);
        }

        public static ConfigFile Instance
        {
            get
            {
                //if (_instance == null)
                //{
                //    _instance = new ConfigFile();
                //}
                return _instance;
            }
        }

        public bool OpenConfigFile()
        {
            string strFileName = FileCommonUtils.SelectFile("GEPS开发配置文件(*.GDE)|*.GDE");

            if (strFileName != "")
            {
                if (LoadConfigFile(strFileName))
                {
                    _ConfigFileName = strFileName;
                    return true;
                }
            }
            return false;
        }

        public bool ImportConfigFile()
        {
            string strFileName = FileCommonUtils.SelectFile("GEPS开发配置文件(*.GDE)|*.GDE");

            if (strFileName != "")
            {
                return LoadConfigFile(strFileName);
            }
            else
            {
                return false;
            }
        }

        public bool LoadConfigFile(string AFileName)
        {
            if(!File.Exists(AFileName))
            {
                return false;
            }
            if ((_Modified) && (_ConfigFile != null))
            {
                MessageBoxResult ret = MessageBox.Show("当前文件已经修改，是否保存", "", MessageBoxButton.YesNoCancel);
                if (ret == MessageBoxResult.Yes)
                {
                    SaveConfigFile();
                }
                if (ret == MessageBoxResult.Cancel)
                    return false;
            }
            if (File.Exists(_ModifyFileName))
            {
                File.Delete(_ModifyFileName);
            }
            _ModifyFileName = Path.GetTempFileName();
            File.Copy(AFileName, _ModifyFileName, true);
            _ConfigFile.Load(_ModifyFileName);
            return true;
        }

        public bool SaveConfigFile()
        {
            if (!File.Exists(_ModifyFileName))
            {
                return false;
            }
            _ConfigFile.Save(_ModifyFileName);
            if (_ConfigFileName != "")
            {
                File.Copy(_ModifyFileName, _ConfigFileName, true);
                return true;
            }
            else
            {
                return SaveAsConfigFile();
            }
        }

        public bool SaveAsConfigFile()
        {
            if (!File.Exists(_ModifyFileName))
            {
                return false;
            }
            _ConfigFile.Save(_ModifyFileName);
            _ConfigFileName = FileCommonUtils.SaveFile("GEPS开发配置文件(*.GDE)|*.GDE");
            if (_ConfigFileName != "")
            {
                File.Copy(_ModifyFileName, _ConfigFileName, true);
                return true;
            }
            return false;
        }

        public XmlNode GetConfigNode(string AID)
        {
            if ((_ConfigFile != null) && (!string.IsNullOrEmpty(AID)))
            {
                return _ConfigFile.SelectSingleNode(AID); 
            }
            return null;
        }

        public string GetConfigText(string ANodePath)
        {
            XmlNode oNode = GetConfigNode(ANodePath);
            return (oNode == null)?"":oNode.InnerText;
        }

        public string GetConfigAttribute(string ANodePath, string Attribute)
        {
            XmlNode oNode = GetConfigNode(ANodePath);
            return (oNode == null) ? "" : oNode.Attributes[Attribute].Value;
        }
    }

}
